library(tidyverse)

S3_d <- read_csv("study3_clean.csv") %>%
  mutate(
    gender = as.factor(gender),
    ai_use = as.factor(ai_use),
    knowl_bin = as.factor(knowl_bin),
    group = as.factor(group)
  )

S3_means <- S3_d %>%
	filter(tr <= 4) %>%
	mutate(group = case_when(
	    tr == 1 ~ "control",
	    tr == 2 ~ "existential",
	    tr == 3 ~ "immediate",
	    tr == 4 ~ "benefits")
	) %>%
	select(c("existential_cap5", "existential_cap10", "existential_occ", "existential_imp", "imminent_cap5", "imminent_cap10", "imminent_occ", "imminent_imp", "positive_cap5", "positive_cap10", "positive_occ", "positive_imp", "group")) %>%
	mutate(across(everything(), ~ haven::zap_labels(.))) %>%
	pivot_longer(
			cols = c("existential_cap5", "existential_cap10", "existential_occ", "existential_imp", "imminent_cap5", "imminent_cap10", "imminent_occ", "imminent_imp", "positive_cap5", "positive_cap10", "positive_occ", "positive_imp"),
    	names_to = c("outcome_type", "outcome_measure"), 
 	    names_sep = "_",
    	values_to = "value"
	) %>%
	group_by(outcome_type, outcome_measure, group) %>%
	summarize(
		mean = mean(value, na.rm = TRUE),
		count = n(),
		se = sd(value, na.rm = TRUE) / sqrt(count)  # Standard error
	) %>%
	mutate(
		lower_ci = mean - qt(0.975, count - 1) * se,
		upper_ci = mean + qt(0.975, count - 1) * se
	) %>%
	mutate(
		outcome_type_label = case_when(
			outcome_type == "existential" ~ "Outcome: existential risks",
			outcome_type == "imminent" ~ "Outcome: immediate risks",
			outcome_type == "positive" ~ "Outcome: benefits",
			TRUE ~ "Other"),
		outcome_measure_label = case_when(
			outcome_measure == "cap10" ~ "Capability",
			outcome_measure == "imp" ~ "Impact",
			outcome_measure == "occ" ~ "Likelihood",
			TRUE ~ "Other"),
		group_label = case_when(
			group == "control" ~ "Control",
			group == "existential" ~ "Existential risks",
			group == "immediate" ~ "Immediate risks",
			group == "benefits" ~ "Benefits")
	) %>%
	mutate(
    outcome_measure_label = factor(outcome_measure_label, levels = c("Capability", "Likelihood", "Impact")),
    outcome_type_label = factor(outcome_type_label, levels = c("Outcome: existential risks", "Outcome: immediate risks", "Outcome: benefits")),
    group_label = factor(group_label, levels = c("Control", "Existential risks", "Immediate risks", "Benefits"))
  )
print(S3_means, n = 100)

S3_means_plot <- S3_means %>%
	filter(outcome_measure %in% c("cap10", "imp", "occ"))

plot_colors <- c("#66c2a5", "#fc8d62", "#8da0cb", "#e78ac3")

figure_1 <- ggplot(data = S3_means_plot) +
  aes(x = outcome_measure_label, y = mean, color = group_label, fill = group_label) +
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), position = position_dodge(width = 0.7), linewidth = 4, width = 0) +
  geom_point(position = position_dodge(width = 0.7), shape = 20, size = 1.5, color = "white") + 
  scale_color_manual(values = plot_colors) +  
  scale_fill_manual(values = plot_colors) + 
  facet_wrap(~ outcome_type_label) +
  labs(x = "", y = "", title = "", color = "", fill = "") +
  guides(color = guide_legend(reverse = FALSE), fill = guide_legend(reverse = FALSE)) +
  theme_light() +
  theme(
    legend.position = "bottom",
    strip.background = element_blank(),
    strip.text = element_text(color = "black", face = "italic"),
    text = element_text(size = 14)
  )
print(figure_1)
ggsave("Figure_1.pdf", figure_1, width = 9, height = 5)
  
